package com.linln.admin.SpringBoot.controller;

import com.linln.admin.SpringBoot.domain.Clean;
import com.linln.admin.SpringBoot.domain.Lab;
import com.linln.admin.SpringBoot.service.CleanService;
import com.linln.admin.SpringBoot.service.LabService;
import com.linln.admin.SpringBoot.validator.CleanValid;
import com.linln.common.enums.StatusEnum;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Airili
 * @date 2020/12/22
 */
@Controller
@RequestMapping("/SpringBoot/clean")
public class CleanController {

    @Autowired
    private CleanService cleanService;

    @Autowired
    private UserService userService;

    @Autowired
    private LabService labService;
    /**
     * 列表页面
     */
    @GetMapping("/index")
    @RequiresPermissions("SpringBoot:clean:index")
    public String index(Model model, Clean clean) {

        // 创建匹配器，进行动态查询匹配
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("user_id", match -> match.contains());

        // 获取数据列表
        Example<Clean> example = Example.of(clean, matcher);
        Page<Clean> list = cleanService.getPageList(example);

        try {
            for(Clean tip:list){
                User user = userService.getByName(tip.getUser_id().toString());
                Lab lab = labService.getById(tip.getLab_id());
                tip.setName(user.getNickname());
                tip.setLab_name(lab.getName());
            }
        }catch (Exception e){
            System.out.println("嘤嘤嘤");
        }


        // 封装数据
        model.addAttribute("list", list.getContent());
        model.addAttribute("page", list);
        return "/SpringBoot/clean/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping("/add")
    @RequiresPermissions("SpringBoot:clean:add")
    public String toAdd(Model model) {
        List<Lab> allLab = labService.findAllLab();
        model.addAttribute("labs",allLab);
        return "/SpringBoot/clean/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("SpringBoot:clean:edit")
    public String toEdit(@PathVariable("id") Clean clean, Model model) {
        model.addAttribute("clean", clean);
        Lab lab = labService.getById(clean.getLab_id());
        List<Lab> allLab = labService.findAllLab();
        model.addAttribute("labs",allLab);
        model.addAttribute("lab_id",lab.getId());
        model.addAttribute("week_day",clean.getWeek_day());
        return "/SpringBoot/clean/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping("/save")
    @RequiresPermissions({"SpringBoot:clean:add", "SpringBoot:clean:edit"})
    @ResponseBody
    public ResultVo save(@Validated CleanValid valid, Clean clean) {
        // 复制保留无需修改的数据
        if (clean.getId() != null) {
            Clean beClean = cleanService.getById(clean.getId());
            EntityBeanUtil.copyProperties(beClean, clean);
        }
//        System.out.println("值日生信息:"+userService.getByName(clean.getUser_id().toString()));
        // 保存数据
//        User user = userService.getByName(clean.getUser_id().toString());
        cleanService.save(clean);
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("SpringBoot:clean:detail")
    public String toDetail(@PathVariable("id") Clean clean, Model model) {
        model.addAttribute("clean",clean);
        return "/SpringBoot/clean/detail";
    }

    /**
     * 设置一条或者多条数据的状态
     */
    @RequestMapping("/status/{param}")
    @RequiresPermissions("SpringBoot:clean:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids) {
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (cleanService.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
}